// pages/pay/porder.js
var app = getApp();
var api = require('../../utils/api.js');
var rsa = require('../../utils/rsa.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsArr:[],
    delBtnWidth: 180,
    totalmoney:0,
    huyflag:false,
    inpttext:'',
    vipdetail:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const tel = options.tel;
    if (tel) {
      this.viprequest(tel);
    }
  },
  showvip:function(){
    var va = this.data.huyflag;
    this.setData({
      huyflag:!va,
    })
  },
  //扫码
  scangood:function(){
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        this.requestGoodsDetail(res.result);
      }
    })
  },
  //删除某一列
  deleCellAction: function (e) {
    var tempArr = this.data.goodsArr;
    const vale = e.currentTarget.dataset.info;
    console.log(vale);
    tempArr.splice(vale, 1);
    this.setData({
      goodsArr: tempArr,
    })
  },

  edit: function (e) {

    //编辑收货地址 省略

  },



  add: function () {

    //增加收货地址 省略

  },



  delItem: function (e) {

    var id = e.currentTarget.dataset.id;

    var index = e.currentTarget.dataset.index;

    this.data.address.splice(index, 1);

    this.setData({

      address: this.data.address

    })
    this.caculateMoney();
  },



  touchS: function (e) {

    if (e.touches.length == 1) {

      this.setData({

        //设置触摸起始点水平方向位置

        startX: e.touches[0].clientX

      });

    }

  },



  touchM: function (e) {

    if (e.touches.length == 1) {

      //手指移动时水平方向位置

      var moveX = e.touches[0].clientX;

      //手指起始点位置与移动期间的差值

      var disX = this.data.startX - moveX;

      var delBtnWidth = this.data.delBtnWidth;

      var txtStyle = "";

      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变

        txtStyle = "left:0rpx";

      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离

        txtStyle = "left:-" + disX + "rpx";

        if (disX >= delBtnWidth) {

          //控制手指移动距离最大值为删除按钮的宽度

          txtStyle = "left:-" + delBtnWidth + "rpx";

        }

      }

      //获取手指触摸的是哪一项

      var index = e.currentTarget.dataset.index;

      var list = this.data.goodsArr;

      list[index]['txtStyle'] = txtStyle;

      //更新列表的状态

      this.setData({

        goodsArr: list

      });

    }

  },

  touchE: function (e) {

    if (e.changedTouches.length == 1) {

      //手指移动结束后水平位置

      var endX = e.changedTouches[0].clientX;

      //触摸开始与结束，手指移动的距离

      var disX = this.data.startX - endX;

      var delBtnWidth = this.data.delBtnWidth;

      //如果距离小于删除按钮的1/2，不显示删除按钮

      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";

      //获取手指触摸的是哪一项

      var index = e.currentTarget.dataset.index;

      var list = this.data.goodsArr;

      var del_index = '';

      disX > delBtnWidth / 2 ? del_index = index : del_index = '';

      list[index].txtStyle = txtStyle;

      //更新列表的状态

      this.setData({

        goodsArr: list,

        del_index: del_index

      });

    }

  },
  //查询商品
  requestGoodsDetail: function (e) {
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, { 'method': 'goods.keyword', "seller_bn": shopnum, 'session_id': settion, "keyword": e }, (err, res) => {
      var redata = res.data.list;
      if (redata.length == 0) {
        wx.showToast({
          title: '未查询到商品',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var good_detail = redata[0];
      var gooArr = this.data.goodsArr;

      var cprice = parseFloat(good_detail.price).toFixed(2);
      var vips = this.data.vipdetail;
      var lev = vips.member_lv_id;
      var gprice = good_detail.member_lv_price;
      for(var i in gprice){
        var gp = gprice[i];
        if (gp.member_lv_id == lev){
          cprice = gp.price;
        }
      }

      var cprice = parseFloat(good_detail.price).toFixed(2);
      var bnum = good_detail.bn
      var tempArr = {
        name: good_detail.name,
        gnum: good_detail.barcode,
        price: cprice,
        amount: good_detail.quantity,
        buyamount: 1,
        id: bnum,
        txtStyle: '',
      };
      gooArr.push(tempArr);
      
      this.setData({
        goodsArr: gooArr,
      })
      this.caculateMoney();
    });
  },
  //结算
  jsAction:function(){
    var gdata = this.data.goodsArr;
    if(gdata.length ==0) return;
    this.orderwithgoods();
  },

  //下单
  orderwithgoods: function () {
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    const vip = this.data.vipdetail;  //vip详情
    var glist = this.data.goodsArr;
    var goodData = [];
    for (var i = 0; i < glist.length; i++) {
      var templist = glist[i];
      var td = { 'good_bn': templist.id, 'num': templist.buyamount};
      goodData.push(td);
    }
    var memberid = vip.member_id ? vip.member_id : '';
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'order.decreate', "seller_bn": shopnum, 'session_id': settion, "goods_data": goodData, 'member_id': memberid,
    }, (err, res) => {
      if (!res || res == '') {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 5000
        })
        return
      }
      var res_data = res.data;
      var oderid = res_data.order_id;
      wx.navigateTo({
        url: '/pages/content/maxpay?ordeid=' + oderid + '&vip=' + vip.mobile,
      })
    });
  },
  //减少-1
  minusgood: function (e) {
    const index = e.currentTarget.dataset.info;
    var arr = this.data.goodsArr;
    var tempdic = arr[index];
    var valu = parseInt(tempdic.buyamount) - 1;
    if (valu == 0) return;
    tempdic.buyamount = valu;
    arr.splice(index, 1, tempdic);
    this.setData({
      goodsArr: arr,
    })
    this.caculateMoney();
  },
  //计算总价
  caculateMoney:function(){
    var arr = this.data.goodsArr;
    var tot = 0;
    for(var i in arr){
      var dic = arr[i];
      var n = parseInt(dic.buyamount);
      var price = dic.price;
      var valu = parseFloat(price)
      tot += valu*n;
    }
    this.setData({
      totalmoney:tot
    })
  },
  //加+1
  addgood: function (e) {
    const index = e.currentTarget.dataset.info;
    var arr = this.data.goodsArr;
    var tempdic = arr[index];
    var valu = parseInt(tempdic.buyamount) + 1;
    if (valu == 0) return;
    tempdic.buyamount = valu;
    arr.splice(index, 1, tempdic);
    this.setData({
      goodsArr: arr,
    })
    this.caculateMoney();
  },
  //确定
  userArrive: function () {
    const tel = this.data.inpttext;
    if (!tel) {
      this.setData({
        vipArr_title: '手机号不为空',
      })
    } else {
      this.setData({
        vipArr_title: '处理中...',
      })
      this.viprequest(tel);

    }
  },
  inpaction: function (e) {
    this.setData({
      inpttext: e.detail.value,
      vipArr_title: '',
    })
  },
  viprequest: function (e) {
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'promotion.member_regist', "seller_bn": shopnum, "session_id": setion, "mobile": e
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        this.setData({
          vipArr_title: err,
        })
        return;
      }
      this.setData({
        vipdetail: res.data, //vip详情
        vipArr_title: '',
        huyflag: false,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})